<?php
class vsControl
{
	var $id = '';
	var $label = '';
	var $ctrlType = 'textbox';	//1=textbox, 2=textarea, 3=password, 4=checkbox,5=radio,6=buttonsubmit,7=buttonreset,8=buttonnone
	var $dataType = 'text';	//number, date

	var $valRequire = true;
	var $notPost = false;
	var $func = '';
	
	var $enabled = '';
	var $readonly = '';
	
	//textbox+textarea
	var $charWidth = '';
	var $maxChar = '';
	var $cols = '';
	var $rows = '';
	var $ctrlVal = '';
	
	//checkbox+radio
	var $checkedValue = '';
	var $checkedStatus = '';	//checked
	
	//button
	var $caption = '';
	var $buttonScript = '';
	
	var $comboboxContent = '';
	
	//style
	var $ctrlStyle = 'font-family: Tahoma,verdana,arial; font-size: 11px';
	var $ctrlClass = '';
	
	function vsControl($id,$ctrlType,$label='',$notPost=false)
	{
		$this->id = $id;
		$this->ctrlType = $ctrlType;
		$this->label = $label;
		if ( strtolower(substr($ctrlType,0,6)) == 'button' )
			$this->caption = $label;
		$this->notPost = $notPost;
	}	
};

class vsForm
{
	//properties
	//importants
	var $gcn;
	var $tblName;
	var $arrFields = array();
	//#######
	var $frmName = '';
	var $actionFile = '';
	var $frmMethod = 'POST';
	var $frmValidate = true;
	var $frmTaget = '_self';
	var $enctype = '';
	var $icon = '';
	var $frmWidth = '100%';
	var $frmAlign = 'center';
	
	var $titleHeight = 25;
	var $title = 'VS Form';
	var $titleBgColor = '#336699';
	var $titleFrColor = '#FFFFFF';
	var $titleFont = 'Tahoma,Verdana,Arial';
	var $titleFontSize = '12';
	var $borderFormStyle = 'border: 1px solid #AAAAAA';
	
	var $frmBodyFont = 'Tahoma,Verdana,Arial';
	var $frmBodyFontSize = 11;
	
	var $buttonPanelHeight = 35;
	var $buttonPanelBgColor = '#000066';
	var $buttonStyle = 'padding-left: 10px; background-color:#FFFFFF; background-image:url(images/check.gif);background-position:left; background-repeat:no-repeat';
	var $buttonPanelAlign = 'right';
	
	var $arrItem = array();
	var $arrButton = array();
	var $arrKeyEdit = array();	//key=field name,data type,val= value
	
	var $uploadPath = 'uploaddatas/';
	var $storePath = 'http://localhost/uploaddatas/';
	
	//method
	
	function vsForm($frmName,$action)
	{
		$this->frmName = $frmName;
		$this->actionFile = $action;
	}
	
	function frmRender()
	{
		$str = '
			<script language="javascript">
				function __vsform_matchVal(val,objID)
				{
					for (i=0; i<document.all[objID].length;i++)
					{
						if ( val == document.all[objID].options[i].value )
							document.all[objID].selectedIndex = i;
					}
				}
							
				function __vs_findobj(n, d) { //v4.01
				  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
					d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
				  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
				  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=__vs_findobj(n,d.layers[i].document);
				  if(!x && d.getElementById) x=d.getElementById(n); return x;
				}
				
				function __vs_validateForm() { //v4.0
				  var i,p,q,nm,test,num,min,max,errors="",args=__vs_validateForm.arguments;
				  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=__vs_findobj(args[i]);
					if (val) { nm=val.name; if ((val=val.value)!="") {
					  if (test.indexOf("isEmail")!=-1) { p=val.indexOf("@");
						if (p<1 || p==(val.length-1)) errors+="- "+nm+" : Dia chi email khong hop le.\n";
					  } else if (test!="R") { num = parseFloat(val);
						if (isNaN(val)) errors+="- "+nm+" : Du lieu phai la so.\n";
						if (test.indexOf("inRange") != -1) { p=test.indexOf(":");
						  min=test.substring(8,p); max=test.substring(p+1);
						  if (num<min || max<num) errors+="- "+nm+" Du lieu phai tu "+min+" den "+max+".\n";
					} } } else if (test.charAt(0) == "R") errors += "- "+nm+" Khong duoc de trong.\n"; }
				  } if (errors) alert("Cac loi can phai sua:\n\n"+errors);
				  document.VS_returnValue = (errors == "");
				}
			</script>
		';
		
		$temp = $this->arrItem;
		$strRequire = ' onSubmit="__vs_validateForm(';
		while ( list($key,$val) = each($temp) )
		{
			if ( $val->valRequire == true )
				$strRequire .= '\'' . $val->id . '\',\'\',\'R\',';
		}
			$strRequire = substr($strRequire,0,strlen($strRequire)-1) . '); return document.VS_returnValue"';
		$str .= '<form name="' . $this->frmName . '" method="' . $this->frmMethod . '" enctype="multipart/form-data" action="' . $this->actionFile . '" ' . $strRequire . '>';
		$str .= '
			<table align="' . $this->frmAlign . '" width="' . $this->frmWidth . '"  border="0" cellspacing="1" cellpadding="0" style="' . $this->borderFormStyle . '">
			  <tr>
				<td height="' . $this->titleHeight . '" bgcolor="' . $this->titleBgColor . '" style="color:' . $this->titleFrColor . ';font-family:' . $this->titleFont . ';font-size:' . $this->titleFontSize . 'px;font-weight:bold; border: 1px outset">
		';
		
		if ( $this->icon != '' )
			$str .= '<img src="' . $this->icon . '" border="0" align="absmiddle">';
		
		$str .= "&nbsp;" . $this->title;
		$str .= '
				</td>
			</tr>
			<tr>
				<td align="center" valign="top">
		';
		
		//start body
		$str .= '
			<table width="100%" border="0" cellspacing="0" cellpadding="0" width="100%">
			<tr><td colspan="2">&nbsp;</td></tr>
		';
		
		while ( list($key,$val) = each($this->arrItem) )
		{
			//moi item la 1 vsControl
			$str .= '<tr>';
			
			switch (strtolower($val->ctrlType))
			{
				case 'textbox':
					$str .= '<td align="right" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">' . $val->label . '</td>';
					$str .= '<td align="left" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">&nbsp;<input type="text" name="' . $val->id . '" class="'. $val->ctrlClass .'" size="' . $val->charWidth . '" maxlength="' . $val->maxChar . '" value="' . $val->ctrlVal . '" ' . $val->enabled . ' ' . $val->readonly . ' style="' . $val->ctrlStyle . '"></td>';
					break;
				case 'textarea';					
					$str .= '<td valign="top" align="right" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">' . $val->label . '</td>';
					$str .= '<td valign=top" align="left" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">&nbsp;<textarea class="'. $val->ctrlClass .'" name="' . $val->id . '" cols="' . $val->cols . '" rows="' . $val->rows . '" ' . $val->enabled . ' ' . $val->readonly . ' style="' . $val->ctrlStyle . '">' . $val->ctrlVal . '</textarea></td>';
					break;
				case 'password':
					$str .= '<td align="right" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">' . $val->label . '</td>';
					$str .= '<td align="left" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">&nbsp;<input type="password" name="' . $val->id . '" class="'. $val->ctrlClass .'" size="' . $val->charWidth . '" maxlength="' . $val->maxChar . '" value="' . $val->ctrlVal . '" ' . $val->enabled . ' ' . $val->readonly . ' style="' . $val->ctrlStyle . '"></td>';
					break;
				case 'checkbox':
					$str .= '<td align="right" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">' . $val->label . '</td>';
					$str .= '<td align="left" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">&nbsp;<input name="' . $val->id . '" type="checkbox" id="' . $val->id . '" value="' . $val->checkedValue . '" ' . $val->checkedStatus . ' /></td>';
					break;
				case 'radio':
					$str .= '<td align="right" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">' . $val->label . '</td>';
					$str .= '<td align="left" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">&nbsp;<input name="' . $val->id . '" type="radio" id="' . $val->id . '" value="' . $val->checkedValue . '" ' . $val->checkedStatus . ' /></td>';
					break;
				case 'file':
					$str .= '<td align="right" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">' . $val->label . '</td>';
					$str .= '<td align="left" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">&nbsp;<input name="' . $val->id . '" class="'. $val->ctrlClass .'" type="file" size="' . $val->charWidth . '" maxlength="' . $val->maxChar . '" id="' . $val->id . '" ' . $val->checkedStatus . ' /></td>';
					break;
				case 'hidden':
					$str .= '<td align="right" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">' . $val->label . '</td>';
					$str .= '<td align="left" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">&nbsp;<input name="' . $val->id . '" type="hidden" id="' . $val->id . '" value="' . $val->ctrlVal . '" /></td>';
					break;					
				case 'combobox':
					$str .= '<td align="right" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">' . $val->label . '</td>';
					$str .= '<td align="left" valign="top" style="font-family:' . $this->frmBodyFont . '; font-size:' . $this->frmBodyFontSize . '">&nbsp;';
					$str .= '<select class="' . $val->ctrlClass . '" name="' . $val->id . '" id="' . $val->id . '">';
					$str .= $val->comboboxContent;
					$str .= '</select>';
					$str .= '</td>';
					break;
			}		
			$str .= '</tr>';
		}
		$str .= '
			  </tr>
			<tr><td colspan="2">&nbsp;</td></tr>			  
			</table>
		';
		//end body
		$strPanel = '';		
		while ( list($key,$val) = each($this->arrButton) )
		{
			switch(strtolower($val->ctrlType))
			{
				case 'buttonsubmit':
					$strPanel .= '<input name="' . $val->id . '" type="submit" id="' . $val->id . '" value="' . $val->caption . '" style="' . $this->buttonStyle . '" onClick="' . $val->buttonScript . '" />';
					break;
				case 'buttonreset':
					$strPanel .= '<input name="' . $val->id . '" type="reset" id="' . $val->id . '" value="' . $val->caption . '" style="' . $this->buttonStyle . '" onClick="' . $val->buttonScript . '" />';
					break;
				case 'buttonnone':
					$strPanel .= '<input name="' . $val->id . '" type="button" id="' . $val->id . '" value="' . $val->caption . '" style="' . $this->buttonStyle . '" onClick="' . $val->buttonScript . '" />';
					break;
			}
		}
		
		$str .= '
				</td>
			  </tr>
			  <tr>
				<td height="' . $this->buttonPanelHeight . '" align="right" bgcolor="' . $this->buttonPanelBgColor . '" style="border: 1px inset">
				<div align="' . $this->buttonPanelAlign . '">&nbsp;			
				' . $strPanel . '
				&nbsp;</div>
				</td>
			  </tr>
			</table>
		';
		$str .= '<input type="hidden" name="hidPOST" value="' . sha1(date("dmYhis")) . '">';
		$str .= '</form>';
		//##################3
		echo $str;
	}
	
	function actionPrevRender($act,$returnPage='')
	{
		if ( @$_POST['hidPOST'] != '' )
		{
			switch(strtolower($act))
			{
				case 'add':
					$sql = "INSERT INTO " . $this->tblName . "(";
					while ( list($key,$fieldName) = each($this->arrFields) )
					{
						$sql .= $fieldName . ',';
					}
					$sql = substr($sql,0,strlen($sql)-1);
					$sql .= ") VALUES(";
					
					$temp = $this->arrItem;
					while ( list($key,$obj) = each($temp) )
					{				
						if ( $obj->notPost == false )
						{
							switch( strtolower($obj->dataType) )
							{
								case 'text':
									$val = (!get_magic_quotes_gpc()) ? addslashes(@$_POST[$obj->id]) : @$_POST[$obj->id];
									if ( empty($val) )	$val = '';
									else
										if ( trim($val->func) != '' ) $val = md5($val);
									$sql .= "'" . $val . "',";
									break;
								case 'number':
									$val = (!get_magic_quotes_gpc()) ? addslashes(@$_POST[$obj->id]) : @$_POST[$obj->id];
									if ( empty($val) )	$val = 0;
									$sql .=  $val . ",";
									break;
								case 'file':
									//print_r( $_FILES[$obj->id]['size']);
									//die();
									/******************************************************************************************/
									//neu co anh -> insert vao db anh
									//qui trinh: upload anh len thu muc tam tren server -> ghi vao db -> xoa anh trong thu muc tam
									$upload_dir = $this->uploadPath;
									$url = '';
									$max_file_upload=return_bytes(ini_get('post_max_size'));
								
									if($_FILES[$obj->id]['size'] > 0 )
									{
										$upload_file=$upload_dir.basename($_FILES[$obj->id]['name']);
										$url = $this->storePath . basename($_FILES[$obj->id]['name']);

										if(file_exists($_FILES[$obj->id]['name']))
										{
											$temp=explode(basename($_FILES[$obj->id]['name']));
											$temp[0].=substr(rand(0,1000),0,2);	
										}
								
										$extension=strtolower(strrchr($upload_file,"."));
								
										/*if(empty($arr_extension[$extension]))
											die('KhÃ´ng Ä‘Ãºng pháº§n má»Ÿ rá»™ng !');*/
								
										if(filesize($_FILES[$obj->id]['tmp_name'])< $max_file_upload)
										{	
											if(is_uploaded_file($_FILES[$obj->id]['tmp_name']))
											{
												if( !move_uploaded_file($_FILES[$obj->id]['tmp_name'],$upload_file) )
													die('Upload không thành công !');
											}
											else 
												die('Không chép file vào thư mục tạm được !');
										}
										else
											die('Kích thước file quá lớn !');
									}
									
									$sql .=  "'" . $url . "',";
									/***************************************************************************************************************/
									break;
							}	
						}					
					}
					$sql = substr($sql,0,strlen($sql)-1);
					$sql .= ")";
					$this->gcn->Execute($sql);
					echo '&#272;&#227; th&#234;m m&#7899;i th&#224;nh c&#244;ng.<br />Vui l&#242;ng ch&#7901; trong gi&#226;y l&#225;t...
					<meta http-equiv="refresh" content="1;URL=' . $returnPage . '" />
					';
					//############
					break;
				case 'edit':
					$sql = "UPDATE " . $this->tblName . " SET ";
	
					$temp = $this->arrItem;
					while ( list($key,$obj) = each($temp) )
					{				
						if ( $obj->notPost == false )
						{
							list($key1,$fieldName) = each($this->arrFields);
							switch( strtolower($obj->dataType) )
							{
								case 'text':
									$val = (!get_magic_quotes_gpc()) ? addslashes(@$_POST[$obj->id]) : @$_POST[$obj->id];
									if ( empty($val) )	$val = '';
									else
										if ( trim($val->func) != '' ) $val = md5($val);
									$sql .= $fieldName . "='" . $val . "',";
									break;
								case 'number':
									$val = (!get_magic_quotes_gpc()) ? addslashes(@$_POST[$obj->id]) : @$_POST[$obj->id];
									if ( empty($val) )	$val = 0;
									$sql .=  $fieldName . "=" . $val . ",";
									break;
							}	
						}					
					}
					$sql = substr($sql,0,strlen($sql)-1);
					$sql .= " WHERE ";
					
					while ( list($key,$val) = each($this->arrKeyEdit) )
					{
						$temp = split(",",$key);
						switch(strtolower($temp[1]))
						{
							case 'text':
								$sql .= $temp[0] . "='" . $val . "' AND " ;
								break;
							case 'number':
								$sql .= $temp[0] . "=" . $val . " AND ";
								break;
						}
					}
					$sql = substr($sql,0,strlen($sql)-4);
					//echo $sql . '<br>';die();
					$this->gcn->Execute($sql);
					
					echo '&#272;&#227; c&#7853;p nh&#7853;t th&#224;nh c&#244;ng.<br />Vui l&#242;ng ch&#7901; trong gi&#226;y l&#225;t...
					<meta http-equiv="refresh" content="1;URL=' . $returnPage . '" />
					';
					//############
					break;
			}
		}
	}
};
?>
